package org.fuys.coder.domain.config.model;

import org.fuys.coder.domain.config.model.req.AppConfigAddVersionReq;
import org.fuys.coder.domain.config.service.BaseConfig;
import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.exception.BaseException;
import org.fuys.coder.common.exception.BusinessException;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.antisepsis.config
 * @className: AppConfigSupport
 * @author: WangJing
 * @description: 配置类型对应的实体类
 * @date: 2024/8/30 12:49
 * @version: 1.0
 */

@Component
public class AppConfigSupport {
    private Map<Integer, BaseConfig> configSetting;

    @Autowired
    public AppConfigSupport(ListableBeanFactory beanFactory){
        configSetting=new ConcurrentHashMap<>();
        final Map<String, BaseConfig> beansOfType = beanFactory.getBeansOfType(BaseConfig.class);
        if(ObjectUtils.isEmpty(beansOfType)){
            throw new BaseException("配置文件信息加载失败! 请检查配置类信息是否存在:BaseConfig实现类缺失");
        }
        beansOfType.forEach((key,value)->{
            configSetting.put(AppConfigType.getIdxByName(key),value);
        });
    }

    public BaseConfig getConfigInstance(Integer type){
        return configSetting.get(type);
    }

    public List<? extends BaseConfig> getTypeConfigList(Integer type){
        if(type==null){
            throw new BusinessException(ResultMessageConstants.ILLEGAL_DATA_FORMAT+"指定配置类型");
        }
        final BaseConfig baseConfig = getBaseConfig(type);
        return baseConfig.selectSubConfig();
    }

    public void switchConfigVersion(Integer type,Integer version){
        if(type==null||version==null){
            throw new BusinessException(ResultMessageConstants.ILLEGAL_DATA_FORMAT+"指定配置具体信息 如版本和类型");
        }
        final BaseConfig baseConfig = getBaseConfig(type);
        baseConfig.switchSubConfigVersion(version);
    }

    public void addConfigVersion(Integer type, AppConfigAddVersionReq req){
        if(type==null){
            throw new BusinessException(ResultMessageConstants.ILLEGAL_DATA_FORMAT+"指定配置类型");
        }
        final BaseConfig baseConfig = getBaseConfig(type);
        baseConfig.addSubConfigVersion(req);
    }

    public void updateConfigValue(Integer type,String key,Object value,Integer version){
        if(type==null||version==null){
            throw new BusinessException(ResultMessageConstants.ILLEGAL_DATA_FORMAT+"指定配置具体信息 如版本和类型");
        }
        final BaseConfig baseConfig = getBaseConfig(type);
        baseConfig.setConfigValue(key,value,version);
    }

    @NotNull
    private BaseConfig getBaseConfig(Integer type) {
        final BaseConfig baseConfig = configSetting.get(type);
        if (baseConfig == null) {
            throw new BusinessException("无法获取此类型配置信息");
        }
        return baseConfig;
    }
}
